Imports System.Collections.ObjectModel
Imports System.Windows.Forms
Public Structure FxRange
    Dim Start As Long
    Dim Length As Long
End Structure
Public Class FxEnum
    Private _Name As String
    Private _Items As Generic.Dictionary(Of Double, String)
    Public Sub New(ByVal aName As String)
        _Items = New Generic.Dictionary(Of Double, String)
        _Name = aName
    End Sub
    Public ReadOnly Property Name() As String
        Get
            Return _Name
        End Get
    End Property

    Public Sub AddItem(ByVal aKey As Double, ByVal aValue As String)
        _Items.Add(aKey, aValue)
    End Sub
    Public Function GetItem(ByVal aKey As Double) As String
        If _Items.ContainsKey(aKey) Then
            Return _Items(aKey)
        End If
        Return Nothing
    End Function
End Class
Public Enum FxStructureAttributeType
    FxByte
    FxWord
    FxDWord
    FxQWord
    FxString
    FxHexBuffer
    FxEnum
    FxFlag
    FxScanTillNullChar
End Enum
Public Class FxStructureAttribute
    Private _Name As String
    Private _Size As Integer
    Private _Type As FxStructureAttributeType
    Private _Buffer As Buffer
    Private _Value As Double
    Private _SizeSource As String
    Private _Enum As String
    Private _Flag As String
    Public Sub New(ByVal aName As String, ByVal aSize As Integer, ByVal aType As FxStructureAttributeType _
                , ByVal aSizeSource As String _
                , ByVal aEnum As String, ByVal aFlag As String)
        _Name = aName
        _Size = aSize
        _Type = aType
        _SizeSource = aSizeSource
        _Enum = aEnum
        _Flag = aFlag
    End Sub
    Public ReadOnly Property Name() As String
        Get
            Return _Name
        End Get
    End Property
    Public ReadOnly Property Size() As Integer
        Get
            Return _Size
        End Get
    End Property
    Public ReadOnly Property Type() As FxStructureAttributeType
        Get
            Return _Type
        End Get
    End Property
    Public ReadOnly Property Buffer() As Buffer
        Get
            Return _Buffer
        End Get
    End Property
    Public ReadOnly Property Value() As Double
        Get
            Return _Value
        End Get
    End Property
    Public ReadOnly Property SizeSource() As String
        Get
            Return _SizeSource
        End Get
    End Property
    Public ReadOnly Property FEnum() As String
        Get
            Return _Enum
        End Get
    End Property
    Public ReadOnly Property Flag() As String
        Get
            Return _Flag
        End Get
    End Property
    Public Shared Function GetAttributeType(ByVal aTypeStr As String) As FxStructureAttributeType
        Select Case aTypeStr
            Case "byte"
                Return FxStructureAttributeType.FxByte
            Case "word"
                Return FxStructureAttributeType.FxWord
            Case "dword"
                Return FxStructureAttributeType.FxDWord
            Case "qword"
                Return FxStructureAttributeType.FxQWord
            Case "string"
                Return FxStructureAttributeType.FxString
            Case "buffer"
                Return FxStructureAttributeType.FxHexBuffer
            Case "enum"
                Return FxStructureAttributeType.FxEnum
            Case "flag"
                Return FxStructureAttributeType.FxFlag
            Case "scan_till_null_char"
                Return FxStructureAttributeType.FxScanTillNullChar
            Case Else
                Throw New Exception("Unknown attribute type : " + aTypeStr)
        End Select
    End Function
End Class


Public Class FxStructure
    Private _Name As String
    Private _Attributes As Generic.List(Of FxStructureAttribute)
    Public Sub New(ByVal aName As String)
        _Name = aName
        _Attributes = New Generic.List(Of FxStructureAttribute)
    End Sub
    Public ReadOnly Property Name() As String
        Get
            Return _Name
        End Get
    End Property
    Public ReadOnly Property Attributes() As ReadOnlyCollection(Of FxStructureAttribute)
        Get
            Return _Attributes.AsReadOnly()
        End Get
    End Property
    Public Sub AppendAttribute(ByRef aAttribute As FxStructureAttribute)
        _Attributes.Add(aAttribute)
    End Sub
End Class

Public Class FxFormatStructures
    Private _Enums As Generic.List(Of FxEnum)
    Private _Structures As Generic.List(Of FxStructure)
    Public Sub New()
        _Enums = New Generic.List(Of FxEnum)
        _Structures = New Generic.List(Of FxStructure)
    End Sub

    Public Sub AddEnum(ByRef aEnum As FxEnum)
        _Enums.Add(aEnum)
    End Sub
    Public Sub AddStructure(ByRef aStructure As FxStructure)
        _Structures.Add(aStructure)
    End Sub
    Public Function GetEnum(ByRef aName As String) As FxEnum
        Dim iEnum As FxEnum
        For Each iEnum In _Enums
            If iEnum.Name = aName Then
                Return iEnum
            End If
        Next
        Return Nothing
    End Function
    Public Function GetStructure(ByRef aName As String) As FxStructure
        Dim iStruct As FxStructure
        For Each iStruct In _Structures
            If iStruct.Name = aName Then
                Return iStruct
            End If
        Next
        Return Nothing
    End Function
End Class

Public Class FxData
    Inherits System.Windows.Controls.TreeViewItem
    Private _Offset As Long
    Private _Size As Integer
    Private _Name As String
    Private _AttrType As FxStructureAttributeType
    Private _AddString As String
    Private _Data As Object

    Public Sub New(ByVal aOffset As Long, ByVal aSize As Integer, ByVal aName As String, ByVal aAttrType As FxStructureAttributeType _
            , ByRef aData As Object, ByVal aAddString As String)
        MyBase.New()
        MyBase.Header = aName
        _Offset = aOffset
        _Size = aSize
        _Name = aName
        _AttrType = aAttrType
        _AddString = aAddString
        _Data = aData
        Foreground = Brushes.White
    End Sub
    Public ReadOnly Property Offset() As Long
        Get
            Return _Offset
        End Get
    End Property
    Public ReadOnly Property Size() As Integer
        Get
            Return _Size
        End Get
    End Property
    Public ReadOnly Property Name() As String
        Get
            Return _Name
        End Get
    End Property
    Public ReadOnly Property Data() As Object
        Get
            Return _Data
        End Get
    End Property
    Public ReadOnly Property AttrType() As FxStructureAttributeType
        Get
            Return _AttrType
        End Get
    End Property
    Public ReadOnly Property AddString() As String
        Get
            Return _AddString
        End Get
    End Property
    Public ReadOnly Property HasChilds() As Boolean
        Get
            If Me.Items.Count > 0 Then
                Return True
            End If
            Return False
        End Get
    End Property
    Public ReadOnly Property Childs() As System.Windows.Controls.ItemCollection
        Get
            If Me.Items.Count > 0 Then
                Return Items
            End If
            Return Nothing
        End Get
    End Property
    Public Sub AppendChild(ByRef aChild As FxData)
        Items.Add(aChild)
    End Sub
    Public Sub UpdateSize()
        Dim iFxData As FxData
        Dim iSize As Integer = 0
        
        For Each iFxData In Items
            iSize = iSize + iFxData.Size
        Next
        _Size = iSize
    End Sub
End Class
